var util = require('../../../../utils/util.js');
var app = getApp();
var today = util.format(new Date(),'yyyy-MM-dd');
Page({
  data: {
    typeList: [{ name: '水费', type: 5, checked: true, thisUse: 0, changeRead: 0 }, { name: '电费', type: 6, checked: true, thisUse: 0, changeRead: 0 }, { name: '煤气费', type: 7, checked: true, thisUse: 0, changeRead: 0 }, { name: '热水费', type: 8, checked: true, thisUse: 0, changeRead: 0 }],
    checkAll:true,
    today: today
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     
     wx.setNavigationBarTitle({
       title: options.roomCode +"室",
     })
    this.setData({
       orderId:options.orderId,
      apartment_id: options.apartment_id,
      apartment_name: options.apartment_name
    })
  },
  /**
   * 全选
   */
  selectalltap: function (e) {    //
    var value = e.detail.value;
    var that = this;
    var showtab = that.data.showtab;
    var checkAll = false;
    if (value && value[0]) {
      checkAll = true;
      let checkboxItems = that.data.typeList;
      for (var i = 0; i < checkboxItems.length; ++i) {
        checkboxItems[i].checked = true;

      }
      that.setData({
        typeList: checkboxItems,
      })
    } else {
      let checkboxItems = that.data.typeList;
      for (var i = 0; i < checkboxItems.length; ++i) {
        checkboxItems[i].checked = false;
      }
     
      that.setData({
        typeList: checkboxItems,
      })
    }
    that.setData({
      'checkAll': checkAll,
    });

  },
  /**
   * 单选
   */
  checkOne: function (e) {
    var that = this;
    let checkboxItems;
    var values = e.detail.value;
    checkboxItems = that.data.typeList;
    for (var i = 0; i < checkboxItems.length; ++i) {
      checkboxItems[i].checked = false;
      for (var j = 0; j < values.length; ++j) {
        if (checkboxItems[i].type == values[j]) {
          checkboxItems[i].checked = true;
          break;
        }
      }
      
    }
    that.setData({
      typeList: checkboxItems,
    })
  },
  /**
   * 本次用量的inputchange事件
   */
  thisUse:function(e){
    var that = this;
    let checkboxItems;
    let types = e.currentTarget.dataset.type;
    var values = e.detail.value;
    checkboxItems = that.data.typeList;
    for (var i = 0; i < checkboxItems.length; ++i) {
      if (checkboxItems[i].type == types) {
          checkboxItems[i].thisUse = values;
        checkboxItems[i].changeRead = values;
          break;
        }
      

    }
    that.setData({
      typeList: checkboxItems,
    })
  },
  /**
   * 修正用量inputchange事件
   */
  thisRead: function (e) {
    var that = this;
    let checkboxItems;
    let types = e.currentTarget.dataset.type;
    var values = e.detail.value;
    checkboxItems = that.data.typeList;
    for (var i = 0; i < checkboxItems.length; ++i) {
      if (checkboxItems[i].type == types) {
        checkboxItems[i].changeRead = values;
        break;
      }


    }
    that.setData({
      typeList: checkboxItems,
    })
  },
  /**
   * 账单点击事件
   */
  submitData:function(e){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定保存吗?',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '保存中请稍后...',
            mask: true,
          })
          that.savesubmit(e,that);

        }
      }
    })
  },
  savesubmit:function(e,that){
    let types = e.currentTarget.dataset.id;//操作类型
    let checkboxItems;
    checkboxItems = that.data.typeList;
    let selData = [];
    for (var i = 0; i < checkboxItems.length; ++i) {
      if (checkboxItems[i].checked == true) {
        selData.push({
          name: checkboxItems[i].name,
          type: checkboxItems[i].type,
          thisUse: checkboxItems[i].thisUse,
          correct_reading: checkboxItems[i].changeRead
        })
        
      }


    }
    wx.getStorage({
      key: 'userInfo',
      success: function (res) {
        that.setData({
          userInfo: res.data
        })
        util.getReq("/app/sdm/subData", {
          source: res.data.source,
          uid: res.data.user_id,
          types: types,
          user_name: res.data.user_name,
          subdata: JSON.stringify(selData),
          order_id: that.data.orderId,
          shoupayDate: today,
          phone: res.data.phone_num,
        }, function (datas) {
          if (datas.code == 0) {
            wx.showToast({
              title: '保存成功',
            })
            setTimeout(function () { 
              wx.redirectTo({
                url: '/pages/home/roomSign/roomSign_list?apartment_id=' + that.data.apartment_id + "&apartment_name=" + that.data.apartment_name,
              })

             }, 1000);
           
          } else {
            wx.hideLoading();
            var errMsg = '服务器异常';
            if (datas.desc) {
              errMsg = datas.desc;
            }
            wx.showModal({
              title: '错误',
              content: datas.desc,
              mode: true,
              showCancel: false,
              confirmColor: '#007aff',//确定文字的颜色
            })
          }

        })
      }, fail: function () {
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }
    });
  },
  /**
   * 选择日期
   */
  bindDateStartChange: function (e) {
    this.setData({
      today: e.detail.value,
    })
  }
})